#include "SchmidtCAM.h"

int main( int argc, char *argv[] ){

    if( argc != 2 ){
        printf("> usage: %s ads5294.ini\n", argv[0]);
        return 0;
    }

    char cfg_ini[1024];
    sprintf(cfg_ini, "%s", argv[1]);
    
    int fd = open("/dev/mem", O_RDWR | O_SYNC);
    uint *ptr1 = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_OUT);
    uint *gpio_reg      = ptr1;     // 0x41200000
    uint *gpio2_reg     = ptr1+2;   // 0x41200008
    
    uint *ptr2 = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_IN);
    uint *gpio_in_reg   = ptr2;     // 0x41210000

    // SPI config
    SPI_CONFIG *spi_config = (SPI_CONFIG*)malloc(sizeof(SPI_CONFIG));
    setup_SPI_by_device_name( spi_config, "ads5294" );

    // print_spi_config( spi_config );
    // exit(0);

    // ADS5294 registers
    ADS5294_Config *ads5294_cfg = (ADS5294_Config*)malloc(sizeof(ADS5294_Config));
    allocate_ads5294_config( ads5294_cfg );

    read_ads5294_config( ads5294_cfg, cfg_ini, "ADS5294");

    // print_ads5294_config( ads5294_cfg );

    // exit(0);

    config_ads5294_via_SPI( gpio_reg, 
					        gpio2_reg, 
					        gpio_in_reg,
					        spi_config,
					        ads5294_cfg );

    free_ads5294_config( ads5294_cfg );

    free(spi_config);
    
    munmap(ptr1,10);
    munmap(ptr2,10);
    close(fd);

    return 0;
}
